﻿#include "rtcaudioframeobserver.h"
#include <QDebug>

namespace ding::rtc {

RTCAudioFrameObserver::RTCAudioFrameObserver(QObject *parent)
    : QObject(parent)
    , m_enabled(false)
{
    // 初始化默认配置
    m_config.sampleRate = RtcEngineAudioSampleRate_48000;
    m_config.channels = RtcEngineStereoAudio;
    m_config.permission = RtcEngineAudioFrameReadOnly;
    
    qDebug() << "[RTC-OBSERVER] RTCAudioFrameObserver created";
}

RTCAudioFrameObserver::~RTCAudioFrameObserver()
{
    qDebug() << "[RTC-OBSERVER] RTCAudioFrameObserver destroyed";
}

void RTCAudioFrameObserver::OnRemoteUserAudioFrame(const char *uid, RtcEngineAudioFrame &frame)
{
    static bool firstFrameLogged = false;
    
    // 只在第一次接收时输出日志
    if (!firstFrameLogged) {
        qDebug() << "[RTC-OBSERVER] First remote user audio frame received - uid:" << uid
                 << "enabled:" << m_enabled
                 << "samples:" << frame.samples
                 << "channels:" << frame.channels
                 << "sampleRate:" << frame.samplesPerSec;
        firstFrameLogged = true;
    }
    
    if (!m_enabled) {
        return;
    }
    
    // 直接转发音频帧，不缓存
    emit audioFrameReceived(QString("remote_%1").arg(uid), frame);
}

void RTCAudioFrameObserver::OnPlaybackAudioFrame(RtcEngineAudioFrame &frame)
{
    static bool firstFrameLogged = false;
    
    // 只在第一次接收时输出日志
    if (!firstFrameLogged) {
        qDebug() << "[RTC-OBSERVER] First playback audio frame received - enabled:" << m_enabled
                 << "samples:" << frame.samples
                 << "channels:" << frame.channels
                 << "sampleRate:" << frame.samplesPerSec;
        firstFrameLogged = true;
    }
    
    if (!m_enabled) {
        return;
    }
    
    // 直接转发音频帧，不缓存
    emit audioFrameReceived("playback", frame);
}

void RTCAudioFrameObserver::OnCapturedAudioFrame(RtcEngineAudioFrame &frame)
{
    static bool firstFrameLogged = false;
    
    // 只在第一次接收时输出日志
    if (!firstFrameLogged) {
        qDebug() << "[RTC-OBSERVER] First captured audio frame received - enabled:" << m_enabled
                 << "samples:" << frame.samples
                 << "channels:" << frame.channels
                 << "sampleRate:" << frame.samplesPerSec;
        firstFrameLogged = true;
    }
    
    if (!m_enabled) {
        return;
    }
    
    // 直接转发音频帧，不缓存
    emit audioFrameReceived("captured", frame);
}

void RTCAudioFrameObserver::OnProcessCapturedAudioFrame(RtcEngineAudioFrame &frame)
{
    static bool firstFrameLogged = false;
    
    // 只在第一次接收时输出日志
    if (!firstFrameLogged) {
        qDebug() << "[RTC-OBSERVER] First processed audio frame received - enabled:" << m_enabled
                 << "samples:" << frame.samples
                 << "channels:" << frame.channels
                 << "sampleRate:" << frame.samplesPerSec;
        firstFrameLogged = true;
    }
    
    if (!m_enabled) {
        return;
    }
    
    // 直接转发音频帧，不缓存
    emit audioFrameReceived("processed", frame);
}

void RTCAudioFrameObserver::OnPublishAudioFrame(RtcEngineAudioFrame &frame)
{
    static bool firstFrameLogged = false;
    
    // 只在第一次接收时输出日志
    if (!firstFrameLogged) {
        qDebug() << "[RTC-OBSERVER] First publish audio frame received - enabled:" << m_enabled
                 << "samples:" << frame.samples
                 << "channels:" << frame.channels
                 << "sampleRate:" << frame.samplesPerSec;
        firstFrameLogged = true;
    }
    
    if (!m_enabled) {
        return;
    }
    
    // 直接转发音频帧，不缓存
    emit audioFrameReceived("publish", frame);
}

void RTCAudioFrameObserver::setEnabled(bool enabled)
{
    bool wasEnabled = m_enabled;
    m_enabled = enabled;
    qDebug() << "[RTC-OBSERVER] RTCAudioFrameObserver enabled changed from" << wasEnabled << "to" << enabled;
    
    if (enabled && !wasEnabled) {
        qDebug() << "[RTC-OBSERVER] Audio frame observer is now ENABLED - expecting audio callbacks";
    } else if (!enabled && wasEnabled) {
        qDebug() << "[RTC-OBSERVER] Audio frame observer is now DISABLED - callbacks will be ignored";
    }
}

void RTCAudioFrameObserver::setConfig(const RtcEngineAudioFrameObserverConfig &config)
{
    m_config = config;
    qDebug() << "[RTC-OBSERVER] RTCAudioFrameObserver config updated:"
             << "sampleRate:" << config.sampleRate
             << "channels:" << config.channels
             << "permission:" << config.permission;
}

} // namespace ding::rtc 